<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 10/24/18
 * Time: 10:13 AM
 */

namespace console\members\wodrow\reverse_proxy;


use yii\base\Component;

class Server extends Component
{
    public function start()
    {
        $_s = new \swoole_server('0.0.0.0', 9501);
        $conf = [
//            'max_conn' => 2,
//            'daemonize' => 1, // 加入此参数后，执行php server.php将转入后台作为守护进程运行
//            'reactor_num' => 4,
//            'worker_num' => 8,
//            'max_request' => 10,
//            'backlog' => 128,
//            'open_cpu_affinity' => 1,
//            'open_tcp_nodelay' => 1,
//            'tcp_defer_accept' => 5,
//            'log_file' => \Yii::getAlias('@runtime/swoole/swoole-serv.log'),
//            'open_eof_check' => true,
//            'package_eof' => "\r\n\r\n", // 设置EOF
//            'heartbeat_check_interval' => 30,
//            'heartbeat_idle_time' => 60, // TCP连接的最大闲置时间，单位s , 如果某fd最后一次发包距离现在的时间超过
//            'dispatch_mode' => 1,
//            'enable_coroutine' => false, // 协程模式
        ];
        $_s->set($conf);
        $_s->on('start', function (\swoole_server $server){
            var_dump($server->master_pid);
            var_dump($server->manager_pid);
        });
        $_s->on('connect', function (\swoole_server $server, $fd, $reactor_id){
            var_dump("connect: {$fd}");
        });
        $_s->on('receive', function (\swoole_server $server, $fd, $reactor_id, $data){
            var_dump("receive: {$fd}, data: {$data}");
            $server->send($fd, "receive to {$fd}: {$data}");
        });
        $_s->on('close', function (\swoole_server $server, $fd, $reactor_id){
            var_dump("close: {$fd}");
        });
        $_s->start();
    }
}